package com.dt.global.utils;


import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.annotation.Scope;

import com.dt.components.exstruts.action.BaseActionSupport;


/**
 * 验证码验证的方法
 * @author MWG
 *
 */
@Scope("prototype")
public class CheckAction  extends BaseActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//json解析对象
	 public  ObjectMapper mapper=new ObjectMapper();
	 /**
	  * 验证码
	  * @return
	  */
	 public String getCheck(){
	 	HttpServletRequest req = 
	 		ServletActionContext.getRequest();
	 	HttpServletResponse resp = 
	 		ServletActionContext.getResponse();
	 	resp.setContentType("text/html;utf-8");
	 	resp.setCharacterEncoding("utf-8");
	 	String value="";
	 	String check="";
	 	try{
	 		// 检查验证码
			HttpSession session = req.getSession();
			String checkCodeS = (String) session
					.getAttribute("code");
		    check=req.getParameter("check");
			if(checkCodeS!=null&&!checkCodeS.equals("")){
				if(check.equalsIgnoreCase(checkCodeS)){
					value="true";
				}
				else{
					value="false";
				}
			}else{
				value="false";
			}
	 		  String json;
	 			json = mapper.writeValueAsString(value);
	 			  PrintWriter out;
	 			  out = resp.getWriter();
	 			  out.print(json);
	 			  out.flush();
	 			  out.close();
	 			  return null;
	 	}catch(Exception e){
	 		e.printStackTrace();
	 		return "error";
	 	}
	 }
}
